package com.fiap.trecho;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.fiap.voo.VooBean;

public class TrechoService implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void createTrecho(VooBean voo) throws SQLException {
		TrechoDao dao = new TrechoDao();
		dao.create(voo);
	}
	
	public void updateTrecho(VooBean voo) throws SQLException{
		TrechoDao dao = new TrechoDao();
		List<TrechoBean> listaTrechosParaAtualizar = new ArrayList<TrechoBean>();
		List<TrechoBean> listaTrechosParaInserir   = new ArrayList<TrechoBean>();
		
		for(TrechoBean t:voo.getListaTrechos()){
			if(t.getId()>0){
				listaTrechosParaAtualizar.add(t);
			}else{
				listaTrechosParaInserir.add(t);
			}
		}
		
		if(listaTrechosParaAtualizar.size()>0){
			voo.setListaTrechos(listaTrechosParaAtualizar);
			dao.update(voo);
		}
		
		if(listaTrechosParaInserir.size()>0){
			voo.setListaTrechos(listaTrechosParaInserir);
			dao.create(voo);
		}
	}

}
